package com.seeyon.apps.taxFund.event;


import com.seeyon.apps.commonExt.util.CommonRestUtil;
import com.seeyon.apps.taxFund.api.ShuiJinEnum;
import com.seeyon.apps.util.CAP4FormKit;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormDataSubBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.workflow.event.AbstractWorkflowEvent;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;
import com.seeyon.ctp.workflow.util.WorkflowEventConstants.WorkflowEventType;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChaiLvBaoXiaoFaPiaoRuZhangEvent extends AbstractWorkflowEvent {

    private static final Logger log = LoggerFactory.getLogger(ChaiLvBaoXiaoFaPiaoRuZhangEvent.class);

    private final CAP4FormManager cap4FormManager;

    public ChaiLvBaoXiaoFaPiaoRuZhangEvent(CAP4FormManager cap4FormManager) {
        this.cap4FormManager = cap4FormManager;
    }


    @Override
    public ApplicationCategoryEnum getAppName() {
        // TODO 自动生成的方法存根
        return ApplicationCategoryEnum.form;
    }

    @Override
    public WorkflowEventType getType() {
        // TODO 自动生成的方法存根
        return WorkflowEventType.Ext;
    }

    @Override
    public String getId() {
        return "chaiLvBaoXiaoFaPiaoRuZhangEvent";
    }

    @Override
    public String getLabel() {
        return "税金-发票入账";
    }

    /*
     * 流程发起前事件
     *
     */
    @Override
    public synchronized WorkflowEventResult onBeforeFinishWorkitem(WorkflowEventData data) {
        log.info("=========================进入-税金-发票入账-onBeforeFinishWorkitem=============================");
        WorkflowEventResult res = null;
        try {
            res = new WorkflowEventResult();
            Map<String, Object> map = data.getBusinessData();
            //获取表单的数据
            FormDataMasterBean master = (FormDataMasterBean) map.get("formDataBean");
            Long formId = master.getFormTable().getFormId();
            FormBean formBean = cap4FormManager.getForm(formId, true);

            FormTableBean dtlTableBean = CAP4FormKit.getSubTableBeanByDisplay(formBean, "票据信息明细表");
            Assert.notNull(dtlTableBean, "【{}】不存在", "票据信息明细表");

            FormTableBean fuKuanDtlTableBean = CAP4FormKit.getSubTableBeanByDisplay(formBean, "付款信息明细表");
            Assert.notNull(fuKuanDtlTableBean, "【{}】不存在", "付款信息明细表");
            List<FormDataSubBean> fukuanDtlRows = master.getSubData(fuKuanDtlTableBean.getTableName());
            Assert.notEmpty(fukuanDtlRows, "【{}】为空", "付款信息明细表");
            String pinZhengHao = CAP4FormKit.getFieldValue(fukuanDtlRows.get(0), "凭证号", String.class);
            Assert.notBlank(pinZhengHao, "凭证号为空");

            Map<String, Object> apiParams = new HashMap<>();
            // 系统来源 必填 sysSource
            apiParams.put("sys_source", "OA");
            List<Map<String, Object>> invoices = new ArrayList<>();
            apiParams.put("invoices", invoices);
            for (FormDataSubBean subData : master.getSubData(dtlTableBean.getTableName())) {
                String nsrsbh = CAP4FormKit.getFieldValue(subData, "纳税人识别号", String.class);
                Map<String, Object> invoice = new HashMap<>();
                // 操作 必填
                invoice.put("operate", CAP4FormKit.getFieldValue(master, "操作", String.class, "0"));
                // 凭证号 必填
                invoice.put("voucher_number", pinZhengHao);
                // 发票号码 必填
                invoice.put("invoice_no", CAP4FormKit.getFieldValue(subData, "发票号码", String.class));
                // 公司税号 必填
                invoice.put("tax_no", nsrsbh);
                // 入账金额 必填
                invoice.put("invoice_amount", CAP4FormKit.getFieldValue(subData, "开票金额", BigDecimal.class));
                // 入账税额 必填
                invoice.put("invoice_tax_amount", CAP4FormKit.getFieldValue(subData, "税额推财务", BigDecimal.class));
                // 发票类型 必填
                invoice.put("invoice_type", CAP4FormKit.getFieldValue(subData, "发票类型", String.class));
                invoices.add(invoice);
            }
            CommonRestUtil.postValid(ShuiJinEnum.INVOICE_SAVE, null, apiParams);
        } catch (Exception e) {
            res.setAlertMessage(StrUtil.format("【{}】异常【{}】", "税金-发票入账", e.getMessage()));
        }
        log.info("=========================退出-税金-发票入账-onBeforeFinishWorkitem=============================");
        return res;
    }
}
